package com.inno.life.application.mq;

import cn.hutool.core.util.IdUtil;
import com.inno.life.common.constant.RocketMQConstant;
import com.inno.life.common.enums.LikedTypeEnum;
import com.inno.life.domain.entity.LikedMessageBO;
import com.inno.life.infra.basic.entity.PlannerMomentLiked;
import com.inno.life.infra.basic.service.PlannerMomentLikedService;
import com.inno.life.infra.basic.service.PlannerMomentService;
import com.inno.lock.processor.annotiation.DistributedLock;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Service;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;

import java.util.Objects;
import java.util.concurrent.TimeUnit;

/**
 * @author sean
 */
@Service
@RocketMQMessageListener(topic = RocketMQConstant.TopicConstant.MOMENT_LIKED_TOPIC, consumerGroup = RocketMQConstant.ConsumerGroupConstant.MOMENT_LIKED_CONSUMER)
@Slf4j
public class LikedMessageListener implements RocketMQListener<LikedMessageBO> {
    @Resource
    private PlannerMomentLikedService plannerMomentLikedService;
    @Resource
    private PlannerMomentService plannerMomentService;
    @Resource
    private DataSourceTransactionManager dataSourceTransactionManager;
    @Resource
    private TransactionDefinition definition;

    @Override
    @DistributedLock(key = "moment_liked_lock",expireTime = 3,timeUnit = TimeUnit.SECONDS)
    public void onMessage(LikedMessageBO likedMessageBO) {
        log.info("点赞消息消费者:{}", likedMessageBO);
        if (Objects.isNull(likedMessageBO)) {
            return;
        }
        Long toMomentId = likedMessageBO.getToMomentId();
        if (LikedTypeEnum.CANCEL.getType().equals(likedMessageBO.getType())) {
            PlannerMomentLiked plannerMomentLiked = this.plannerMomentLikedService.queryByFromIdAndToMomentId(likedMessageBO.getFromId(), toMomentId);
            if (Objects.nonNull(plannerMomentLiked)) {
                TransactionStatus cancelStatus = dataSourceTransactionManager.getTransaction(definition);
                try {
                    this.plannerMomentLikedService.deleteById(plannerMomentLiked.getId());
                    this.plannerMomentService.decrementLikeCount(toMomentId);
                    dataSourceTransactionManager.commit(cancelStatus);
                } catch (Exception e) {
                    log.error("取消点赞事务回滚", e);
                    dataSourceTransactionManager.rollback(cancelStatus);
                }
                return;
            }
            return;
        }

        PlannerMomentLiked plannerMomentLiked = this.plannerMomentLikedService.queryByFromIdAndToMomentId(likedMessageBO.getFromId(), toMomentId);
        if (Objects.nonNull(plannerMomentLiked)) {
            return;
        }
        TransactionStatus insertStatus = dataSourceTransactionManager.getTransaction(definition);
        try {
            PlannerMomentLiked entity = new PlannerMomentLiked();
            entity.setId(IdUtil.getSnowflakeNextId());
            entity.setCreateTime(likedMessageBO.getCreateTime());
            entity.setFromId(likedMessageBO.getFromId());
            entity.setToMomentId(toMomentId);
            this.plannerMomentLikedService.insert(entity);
            this.plannerMomentService.incrementLikeCount(toMomentId);
            dataSourceTransactionManager.commit(insertStatus);
        } catch (Exception e) {
            log.error("点赞事务回滚", e);
            dataSourceTransactionManager.rollback(insertStatus);
        }

    }
}
